package com.ittsang.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ittsang.dto.ClubApplicationDTO;
import com.ittsang.dto.PageDTO;
import com.ittsang.dto.R;
import com.ittsang.dto.UserDTO;
import com.ittsang.mapper.ClubApplicationMapper;
import com.ittsang.mapper.ClubMapper;
import com.ittsang.mapper.UserClubPostsMapper;
import com.ittsang.mapper.UserMapper;
import com.ittsang.pojo.Club;
import com.ittsang.pojo.ClubApplication;
import com.ittsang.pojo.UserClubPosts;
import com.ittsang.service.clubApplicationService;
import com.ittsang.utils.UserHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class clubApplicationServiceImpl implements clubApplicationService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private ClubApplicationMapper clubApplicationMapper;

    @Resource
    private ClubMapper clubMapper;

    @Resource
    private UserClubPostsMapper userClubPostsMapper;

    @Override
    public R getAuditClub(PageDTO pageDTO) {
        UserDTO user = UserHolder.getUser();
        if (user.getRoleId().equals(0)) {
            return R.error("用户无权限");
        }
        List<ClubApplication> clubApplications = clubApplicationMapper.getAllClubApplication();

        for (ClubApplication clubApplication : clubApplications) {
            clubApplication.setUserName(userMapper.getUserName(clubApplication.getUserId()));
        }
        int pageNum = pageDTO.getPageNum();
        int pageSize = pageDTO.getPagesize();
        PageHelper.startPage(pageNum,pageSize);
        int skip= (pageNum-1)*pageSize;
        Page<ClubApplication> page = new Page<>(pageNum, pageSize);
        page.setTotal(clubApplications.size());
        List<ClubApplication> articleDTOList = clubApplications.stream().skip(skip)
                .limit(pageSize).collect(Collectors.toList());
        page.addAll(articleDTOList);

        return R.ok("获取成功").put("clubApplications", clubApplications).put("total", page.getTotal());
    }

    @Override
    public R clubApplication(ClubApplicationDTO clubApplicationDTO) {
        UserDTO user = UserHolder.getUser();
        if (user.getClubId() != 0) {
            return R.error("该用户不能申请社团");
        }
        clubApplicationDTO.setUserId(Math.toIntExact(user.getId()));
        boolean i = clubApplicationMapper.insertData(clubApplicationDTO);
        return i ? R.ok("添加成功，等待管理员审核") : R.error("添加失败");
    }

    @Override
    @Transactional
    public R auditClubApplication(Integer clubApplicationId) {
        UserDTO user = UserHolder.getUser();
        if (user.getRoleId().equals(0)) {
            return R.error("用户无权限");
        }
        // 查询创建内容
        ClubApplication clubApplication = clubApplicationMapper.getClubApplicationById(clubApplicationId);
        Club club = new Club();
        club.setName(clubApplication.getName());
        club.setDetail(clubApplication.getDetail());
        club.setSrc("");
        // 创建新的社团
        clubMapper.insert(club);
        // 修改用户的社团id
        userMapper.alterUserClub(clubApplication.getUserId(), 0, club.getId());
        // 新建会长职位
        UserClubPosts userClubPosts = new UserClubPosts();
        userClubPosts.setUserId(clubApplication.getUserId());
        userClubPosts.setClubId(club.getId());
        userClubPosts.setPostsId(0);
        userClubPostsMapper.insert(userClubPosts);
        // 删除申请信息
        boolean i = clubApplicationMapper.deletdClubApplication(clubApplicationId);
        return i ? R.ok("通过") : R.error("通过失败");
    }

    @Override
    public R auditNoClubApplication(Integer clubApplicationId) {
        UserDTO user = UserHolder.getUser();
        if (user.getRoleId().equals(0)) {
            return R.error("用户无权限");
        }
        boolean i = clubApplicationMapper.deletdClubApplication(clubApplicationId);
        return i ? R.ok("不通过") : R.error("不通过失败");
    }
}
